package com.sfdk.admin.data.battleAnimationEditor
{
    import com.sfdk.admin.data.RectangleEx;

    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.geom.Matrix;

    import mx.events.PropertyChangeEvent;

    [Bindable]
    public class BattleAnimationFrame extends EventDispatcher
    {
        public var id:int = -1;

        public var sourceRect:RectangleEx;

        private var _parentFrame:BattleAnimationFrame;

        private var _xOffset:int;

        private var _yOffset:int;


        public function get xOffset():int {
            if (parentFrame)
                return parentFrame.xOffset;
            else
                return _xOffset;
        }

        public function set xOffset(value:int):void {
            if (parentFrame)
                parentFrame.xOffset = value;
            else
                _xOffset = value;
        }

        public function get yOffset():int {
            if (parentFrame)
                return parentFrame.yOffset;
            else
                return _yOffset;
        }

        public function set yOffset(value:int):void {
            if (parentFrame)
                parentFrame.yOffset = value;
            else
                _yOffset = value;
        }

        private var _sourceBitmapData:BitmapData;

        public function get sourceBitmapData():BitmapData {
            return _sourceBitmapData;
        }

        public function set sourceBitmapData(value:BitmapData):void {
            _sourceBitmapData = value;
            refresh();
        }

        private var _bitmapData:BitmapData;

        public function get bitmapData():BitmapData {
            if (parentFrame)
                return parentFrame.bitmapData;
            else
                return _bitmapData;
        }

        public function set bitmapData(value:BitmapData) {
             _bitmapData = value;
        }

        public function refresh():void {
            var middleValue:BitmapData = _sourceBitmapData;
            if (_horizontalFlip && !_verticalFlip) {
                var reverseMatrix:Matrix = new Matrix();
                reverseMatrix.scale(-1, 1);
                reverseMatrix.translate(middleValue.width, 0);
                var newBitmapData:BitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }
            if (_verticalFlip && !_horizontalFlip) {
                reverseMatrix = new Matrix();
                reverseMatrix.scale(1, -1);
                reverseMatrix.translate(0, middleValue.height);
                newBitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }
            if (_verticalFlip && _horizontalFlip) {
                reverseMatrix = new Matrix();
                reverseMatrix.scale(-1, -1);
                reverseMatrix.translate(middleValue.width, bitmapData.height);
                newBitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }

            bitmapData = middleValue;

            dispatchEvent(new Event(Event.CHANGE));
            for each(var childFrame : BattleAnimationFrame in _childrenList)
            {
                childFrame.dispatchEvent(PropertyChangeEvent.createUpdateEvent(childFrame, "bitmapData", null, null));
            }
        }


        private var _horizontalFlip:Boolean;

        public function get horizontalFlip():Boolean {
            if (parentFrame)
                return parentFrame.horizontalFlip;
            else
                return _horizontalFlip;
        }

        public function set horizontalFlip(value:Boolean):void {
            if (parentFrame)
            {
                parentFrame.horizontalFlip = value;
            }
            else {
                _horizontalFlip = value;
                refresh();
            }
        }


        private var _verticalFlip:Boolean;

        public function get verticalFlip():Boolean {
            if (parentFrame)
                return parentFrame.verticalFlip;
            else
                return _verticalFlip;
        }

        public function set verticalFlip(value:Boolean):void {
            if (parentFrame)
            {
                parentFrame.verticalFlip = value;
            }
            else {
                _verticalFlip = value;
                refresh();
            }

        }

        public function get parentFrame():BattleAnimationFrame {
            return _parentFrame;
        }

        public function set parentFrame(value:BattleAnimationFrame):void {
            if(_parentFrame)
            {
                _parentFrame.removeChild(this);
            }
            _parentFrame = value;
            _parentFrame.addChild(this);
        }

        private var _childrenList : Vector.<BattleAnimationFrame> = new Vector.<BattleAnimationFrame>();

        public function removeChild(childFrame : BattleAnimationFrame) : void
        {
            var idx : int = _childrenList.indexOf(childFrame);
            if(idx != -1)
            {
                _childrenList.splice(idx, 1);
            }
        }

        public function addChild(childFrame : BattleAnimationFrame) : void
        {
            _childrenList.push(childFrame);
        }
    }
}
